package com.eascs.web.o2o.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.o2o.web.entity.O2oPlatformRegional;
import com.eascs.web.o2o.web.form.ChannelBusStatisForm;
import com.eascs.web.o2o.web.service.ChannelBusStatisService;
import com.eascs.web.o2o.web.service.ProvinceNameData;

@RestController
@RequestMapping("/restChannel")
public class ChannelBusStatisRestController {
	
	@Autowired
	ChannelBusStatisService channelBusStatisService;
	
	@Autowired
	ProvinceNameData provinceNameData;

	@RequestMapping("/o2o")
	public PaginationRestResult o2o(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/o2o";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String partnerName = entity.getPartnerName() == null ? "" : entity.getPartnerName();
			String partnerType = entity.getPartnerType() == null ? "" : entity.getPartnerType();
			String loanerName = entity.getLoanerName() == null ? "" : entity.getLoanerName();
			String loanDate = entity.getLoanDate()== null ? "" : entity.getLoanDate();
			String loanDateEnd = entity.getLoanDateEnd()== null ? "" : entity.getLoanDateEnd();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerType", partnerType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanerName", loanerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDate", loanDate);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = o2ourl + "&pageIndex=";
		} else {
			o2ourl = o2ourl + "?pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = channelBusStatisService.getChannelBusStatisByPage(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"贷款单号", "贷款类型", "借款人","期限","金额(万元)","信贷经理", "经办平台", "渠道类型", "渠道名称","渠道经办人", "推荐类型", "内部推荐人","放款时间"};
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, null,null ,new SelectDataOption("OTO_LOAN_TYPE"), null, null,null,null,null};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	@RequestMapping("/o2o_1")
	public PaginationRestResult o2o1(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/o2o_1";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String partnerName = entity.getPartnerName() == null ? "" : entity.getPartnerName();
			String partnerType = entity.getPartnerType() == null ? "" : entity.getPartnerType();
			String loanerName = entity.getLoanerName() == null ? "" : entity.getLoanerName();
			String loanDate = entity.getLoanDate()== null ? "" : entity.getLoanDate();
			String loanDateEnd = entity.getLoanDateEnd()== null ? "" : entity.getLoanDateEnd();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerType", partnerType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanerName", loanerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDate", loanDate);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = o2ourl + "&pageIndex=";
		} else {
			o2ourl = o2ourl + "?pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = channelBusStatisService.getChannelBusStatisByPage1(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"贷款单号", "贷款类型", "借款人","期限","金额(万元)","信贷经理", "经办平台", "渠道类型", "渠道名称","渠道经办人", "推荐类型", "内部推荐人","放款时间"};
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, null,null ,new SelectDataOption("OTO_LOAN_TYPE"), null, null,null,null,null};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	
	
	
	//===========================================
	@RequestMapping("/brand_channel")
	public PaginationRestResult brandStatis(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/brand_channel";
		if (entity != null) {
			String loanDateS = entity.getLoanDateS()== null ? "" : entity.getLoanDateS();
			 String loanDateE= entity.getLoanDateE()== null ? "" : entity.getLoanDateE();
			 String expireDateS= entity.getExpireDateS()== null ? "" : entity.getExpireDateS();
			 String expireDateE = entity.getExpireDateE()== null ? "" : entity.getExpireDateE();
			 String brandName= entity.getBrandName()== null ? "" : entity.getBrandName();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 String province= entity.getProvince()== null ? "" : entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateS", loanDateS);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateE", loanDateE);
			o2ourl = UrlUtil.appendUrl(o2ourl, "expireDateS", expireDateS);
			o2ourl = UrlUtil.appendUrl(o2ourl, "expireDateE", expireDateE);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&pageIndex=";
		} else {
			o2ourl = o2ourl + "?pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = channelBusStatisService.getBrandChannelBusStatisByPage(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"品牌", "渠道", "渠道通过时间","省区(渠道所在)","地级市(渠道所在)", "省区(借款人所在)", "地级市(借款人所在)","区(借款人所在)", "贷款单号", "投放金额(万元)"};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	
	
	
	
	@RequestMapping("/restViewLoanData")
	public PaginationRestResult restViewLoanData(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/restViewLoanData";
		if (entity != null) {
			String loanDateS = entity.getLoanDateS()== null ? "" : entity.getLoanDateS();
			String loanDateE= entity.getLoanDateE()== null ? "" : entity.getLoanDateE();
			String province= entity.getProvince()== null ? "" : entity.getProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateS", loanDateS);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateE", loanDateE);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = o2ourl + "&pageIndex=";
		} else {
			o2ourl = o2ourl + "?pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = channelBusStatisService.getRestViewLoanDataByPage(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"省区", "贷款单号", "放款日期","放款金额（万元）","放款期限","信贷经理名称"};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	
	
	
	@RequestMapping("/ownDetail")
	public PaginationRestResult ownDetail(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/ownDetail";
		return detail(o2ourl,entity,"0");
	}
	@RequestMapping("/commonDetail")
	public PaginationRestResult commonDetail(ChannelBusStatisForm entity) {
		String o2ourl = "/restChannel/commonDetail";
		return detail(o2ourl,entity,"1");
	}
	
	
	@RequestMapping(value = "/channelBillCode")
	public PaginationRestResult channelBillCode(ChannelBusStatisForm entity) {
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = channelBusStatisService.getChannelforBill(entity);
		String[] titles = {  "贷款单号", "借款人", "省区" ,"信贷经理","放款金额（万元）","放款日期","推荐类型","推荐人"};
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, "/restChannel/channelBillCode?showPage=");
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(0);		
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
	
	public PaginationRestResult detail(String o2ourl,ChannelBusStatisForm entity,String type) {
		if (entity != null) {
			String partnerName = entity.getPartnerName() == null ? "" : entity.getPartnerName();
			String partnerType = entity.getPartnerType() == null ? "" : entity.getPartnerType();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String partnerStartDate = entity.getPartnerStartDate()== null ? "" : entity.getPartnerStartDate();
			String partnerEndDate = entity.getPartnerEndDate()== null ? "" : entity.getPartnerEndDate();
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerType", partnerType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerStartDate", partnerStartDate);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerEndDate", partnerEndDate);
			o2ourl = o2ourl + "&pageIndex=";
		} else {
			o2ourl = o2ourl + "?pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = null;
		if("0".equals(type)){//自己看自己数据
			pageResult = channelBusStatisService.getChannelListForOwnByPage(entity);
		}else{//总部看所有，省区看自己省区
			pageResult = channelBusStatisService.getChannelListForCommonByPage(entity);
		}
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"编码", "渠道主办省", "渠道类型","渠道名称","渠道生效时间","业务单量", "放款总额(万元)"};
		Button[] titleButton = new Button[] {ButtonHelper.createOpenButton(null, "/channel/channelDetail?partnerCode=")};
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("OTO_LOAN_TYPE"), null, null,null,null};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setTitleButtons(titleButton);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setRendTo("#result_list");
		O2oPlatformRegional q= new O2oPlatformRegional();
		q.setProvinceCode("1");
		q.setProvinceName("总部");
		List<O2oPlatformRegional> proList = provinceNameData.getProvinceNameData();
		proList. add(0,q);
		ViewContext.put("proList", proList);
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	
	
	
	
	
	
	
	
}
